package com.example.routeplansystem.exception;

import com.example.routeplansystem.constant.ErrorMessage;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;

/**
 * @author LuCong
 * @since 2020-09-22
 **/
@RestControllerAdvice
@Order(Integer.MAX_VALUE - 1)
public class GlobalExceptionHandler {

    public GlobalExceptionHandler() {
    }

    @ExceptionHandler({ServiceException.class})
    @ResponseStatus(HttpStatus.OK)
    public ErrorMessage onServiceException(HttpServletRequest request, HttpServletResponse response, ServiceException exception) {

        return new ErrorMessage(exception.getCode(), exception.getMessage(), exception.getFields());
    }

    @ExceptionHandler({Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ErrorMessage onUnknownException(HttpServletRequest request, HttpServletResponse response, Exception exception) {

        return new ErrorMessage("1000", exception.getMessage());
    }

    private String toString(ConstraintViolation<?> violation) {
        return violation.getMessage();
//        return violation.getPropertyPath() + "校验失败：" + violation.getMessage();
    }
}
